package mms

import com.lucastex.grails.fileuploader.UFile
import grails.converters.JSON

class PersonController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        [personInstanceList: Person.list(params), personInstanceTotal: Person.count()]
    }

    def search = {
            params.sort = "id"
        params.order ="desc"
        params.max = Math.min(params.max ? params.int('max') : 10, 100)

        if (params.keyword) {
             [personInstanceList: Person.findAllByNameIlike("%${params.keyword}%",params), 
                personInstanceTotal: Person.countByNameIlike("%${params.keyword}%"),
                keyword :params.keyword]
        }else{
            [personInstanceList: Person.list(params), personInstanceTotal: Person.count()]
        }
    }



    def deleteAttachment = {
//        log.info "articleAjax.deleteAttachment params.id = ${params.id}"
//        def ufile = UFile.get(params.id)
//        if (ufile) {
//            try {
//                def article = Article.get(params.articleId)
//                if (article) {
//                    article.removeFromAttachments(ufile)
//                    article.save(flush: true) 
//                }
// 
//                ufile.delete(flush: true)
//
//                new File(ufile.path).delete() 
//                return render(['success': true ,'message' : '删除成功。'] as JSON) 
//
//            }
//            catch (Exception e) {
//                log.info "articleAjax.deleteAttachment Exception"
//                 return render(['success': false ,'message' : '删除失败。'] as JSON) 
//            }
//        }else{
//            log.info "articleAjax.deleteAttachment 没找到"
//             return render(['success': false ,'message' : '没找到这个附件。'] as JSON) 
//        }
    }

//15829901550 wangqian

 
    def create = {
        def personInstance = new Person()
        personInstance.properties = params
        return [personInstance: personInstance]
    }

    def save = {
        def personInstance = new Person(params)

        log.info "personInstance.multimedia = ${personInstance.multimedia}"
        if (personInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'person.label', default: 'Person'), personInstance])}"
            //redirect(action: "show", id: personInstance.id)
            def returnScript = "window.parent.save(${personInstance.id},'${personInstance}');window.parent.tb_remove();";
            render(returnScript.encodeAsJs());
        }
        else {
            render(view: "create", model: [personInstance: personInstance])
        }
    }

    def show = {
        def personInstance = Person.get(params.id)
        if (!personInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'person.label', default: 'Person'), params.id])}"
            redirect(action: "list")
        }
        else {
            [personInstance: personInstance]
        }
    }

    def edit = {
        def personInstance = Person.get(params.id)
        if (!personInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'person.label', default: 'Person'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [personInstance: personInstance]
        }
    }

    def update = {
        def personInstance = Person.get(params.id)
        if (personInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (personInstance.version > version) {
                    
                    personInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'person.label', default: 'Person')] as Object[], "Another user has updated this Person while you were editing")
                    render(view: "edit", model: [personInstance: personInstance])
                    return
                }
            }
            personInstance.properties = params
            if (!personInstance.hasErrors() && personInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'person.label', default: 'Person'), personInstance])}"
                redirect(action: "show", id: personInstance.id)
            }
            else {
                render(view: "edit", model: [personInstance: personInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'person.label', default: 'Person'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def personInstance = Person.get(params.id)
        if (personInstance) {
            try {
                personInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'person.label', default: 'Person'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'person.label', default: 'Person'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'person.label', default: 'Person'), params.id])}"
            redirect(action: "list")
        }
    }
}
